package pacMan;

/**
 * Class: Coordinate
 * 
 * Tracks the position of a point in a 2D X-Y coordinate system.
 *  
 * @author 		Tomas Rivera
 * @version 	$Id$
 *
 */
public class Coordinate {

	//////////////////////////////////////////////////////////
	// Constructor methodss
	//////////////////////////////////////////////////////////
	
	/**
	 * Creates a Coordinate object that tracks where
	 * to position a painted object within a JPanel.
	 * 
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public Coordinate(int x, int y, int w, int h) {
		xCo = x;
		yCo = y;
		width = w;
		height = h;
	}
	
	/**
	 * Creates a Coordinate object that tracks where
	 * to position a painted object within a JPanel.
	 * 
	 * @param x
	 * @param y
	 * @param windowParams
	 */
	public Coordinate(int x, int y, WindowParameters windowParams) {
		xCo = x;
		yCo = y;
		width = windowParams.getWindowWidth();
		height = windowParams.getWindowHeight();
	}
	
	/**
	 * Creates a Coordinate object that tracks where
	 * to position a painted object within a window.
	 * 
	 * @param windowParams
	 */
	public Coordinate(WindowParameters windowParams) {
		xCo = 0;
		yCo = 0;
		width = windowParams.getWindowWidth();
		height = windowParams.getWindowHeight();
	}
	
	//////////////////////////////////////////////////////////
	// Public methods
	//////////////////////////////////////////////////////////
	
	/**
	 * Makes a new copy of a Coordinate object.
	 * 
	 * @return
	 */
	public Coordinate cloneCoordinateObject() {
		return new Coordinate(getX(), getY(), width, height);
	}
	
	/**
	 * Recalculates the position of the Coordinate object
	 * using the new window width and height.
	 * 
	 * @param w - resized window width
	 * @param h - resized window height
	 */
	public void update(int w, int h) {
		if(w != width) {
			xCo = xCo * w / width;
			width = w;
		}
		
		if(h != height) {
			yCo = yCo * h / height;
			height = h;
		}
	}
	
	/**
	 * Get pixel X position in JPanel.
	 * 
	 * @return
	 */
	public int getX() { return xCo; }
	
	/**
	 * Get pixel Y position in JPanel.
	 * 
	 * @return
	 */
	public int getY() { return yCo; }
	
	/**
	 * Repositions coordinate to a non-adjacent location.
	 * 
	 * @param x
	 * @param y
	 */
	public void changePosition(int x, int y) {
		xCo = x;
		yCo = y;
	}
	
	/**
	 * Shifts a coordinate N pixels WEST.
	 * 
	 * @param pixels
	 */
	public void moveLeft(int pixels) {
		xCo = xCo - pixels;
	}
	
	/**
	 * Shifts a coordinate N pixels EAST.
	 * 
	 * @param pixels
	 */
	public void moveRight(int pixels) {
		xCo = xCo + pixels;
	}
	
	/**
	 * Shifts a coordinate N pixels NORTH.
	 * 
	 * @param pixels
	 */
	public void moveUp(int pixels) {
		yCo = yCo - pixels;
	}
	
	/**
	 * Shifts a coordinate N pixels SOUTH.
	 * 
	 * @param pixels
	 */
	public void moveDown(int pixels) {
		yCo = yCo + pixels;
	}
	
	/**
	 * Checks if the coordinate object passed in represents the same x,y
	 * location as this object.
	 * 
	 * @param co
	 * @return
	 */
	public boolean locationMatch(Coordinate co) {
		return (co.getX() == this.getX() && co.getY() == this.getY());
	}
	
	//////////////////////////////////////////////////////////
	// Private member variables
	//////////////////////////////////////////////////////////
	
	private int xCo;
	private int yCo;
	private int width;
	private int height;

}
